package com.authine.cloudpivot.controller;


import com.authine.cloudpivot.foundation.orm.api.dml.JPANativeQuery;
import com.authine.cloudpivot.util.Result;
import com.authine.cloudpivot.web.api.controller.base.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/list")
@Slf4j
public class GetListController extends BaseController {

    @Autowired
    private JPANativeQuery jpaNativeQuery;


    @GetMapping("/getList")
    public Map<String,Object> getList(@RequestParam int page, @RequestParam int size) {
        String userId = getUserId();
        log.info("用户id:{}", userId);
//        String userId = "2c9b50438bad87c8018badd8763f0736";
        String depsql = String.format("SELECT departmentId as depId FROM h_org_user  where id = '%s' ",userId);
        List<Map<String,Object>> list =jpaNativeQuery.getList(depsql, null, null);
        String departmentId =(String) list.get(0).get("depId");
        log.info("用户部门id：{}",departmentId);
        String sql =String.format("select * from iknn4_resident where belongJd ='%s' or hjjd ='%s' limit %s,%s",departmentId,departmentId,page,size);
        String countSql =String.format("select count(*) as total from iknn4_resident where belongJd ='%s' or hjjd ='%s'",departmentId,departmentId);
        List list1 = jpaNativeQuery.getList(sql, null, null);
        List<Map<String,Object>> list2 = jpaNativeQuery.getList(countSql, null, null);
        Map<String,Object> map = new HashMap<>();
        map.put("data",list1);
        map.put("count",list2.get(0).get("total"));
        return map;
    }
}
